﻿<?php
/*删除订单接口*/ 
include("conn.php");
mysql_query('SET NAMES UTF8'); 

$ordersid = $_REQUEST['ordersid'];

$result_data_array = array();
if(!$ordersid){
	$result_data_array['code'] = -100;
	$result_data_array['msg'] = '订单信息错误';
	echo json_encode($result_data_array);
	exit;
}

mysql_query("BEGIN");
mysql_query("COMMIT"); 
mysql_query("END");
//如果这个订单未付款的话，首单返券加入金额
$order_info_sql = "SELECT state, first_couponId, first_coupon_price FROM goodsorders WHERE ordersid = " . $ordersid;
// $goodsorders_sql = "DELETE FROM goodsorders where ordersid = $ordersid";
$goodsorders_sql = "UPDATE goodsorders set is_delete = 0 WHERE ordersid = " . $ordersid;
// $car_sql = "DELETE FROM goodsorders where ordersid = $ordersid";
$car_sql = "UPDATE goodsorders set is_delete = 0 WHERE ordersid = " . $ordersid;

$order_info = mysql_fetch_assoc(mysql_query($order_info_sql));
if($order_info['state'] == 0 && $order_info['first_couponId'] != 0) {
    $sql = "SELECT amount, lastAmount FROM orders_users_first WHERE id = " . $order_info['first_couponId'];
    $lastAmount = mysql_fetch_assoc(mysql_query($sql));
    if($lastAmount == null) {
        $lastAmount = $lastAmount['amount'];
    } else {
        $lastAmount = $lastAmount['lastAmount']+$order_info['first_coupon_price'];
    }
    $sql = "UPDATE orders_users_first set lastAmount = " . $lastAmount . " WHERE id = " . $tuihuo['first_couponId'];
    mysql_query($sql);
}

$goodsorders_query = mysql_query($goodsorders_sql);
if(!$goodsorders_query){
	mysql_query("ROLLBACK"); 
	mysql_query("END");
	$result_data_array['code'] = -100;
	$result_data_array['msg'] = '删除订单失败';
	echo json_encode($result_data_array);
	exit;
}

$car_query = mysql_query($car_sql);
if(!$car_query){
	mysql_query("ROLLBACK"); 
	mysql_query("END"); 
	$result_data_array['code'] = -100;
	$result_data_array['msg'] = '删除订单失败';
	echo json_encode($result_data_array);
	exit;
}

$result_data_array['code'] = 200;
$result_data_array['msg'] = '删除订单成功';
echo json_encode($result_data_array);
